package com.example.demo.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;

import java.io.*;

/**
 * Criteria工具类
 * 
 * @author jqlin
 *
 */
public class CriteriaUtils {

    private static final Log logger = LogFactory.getLog(CriteriaUtils.class);

    /**
     * 复制指定的criteria对象
     * @param criteria criteria对象
     * @return 复制的criteria对象
     */
    public static DetachedCriteria clone(DetachedCriteria criteria) {
        try {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            ObjectOutputStream outputStream = new ObjectOutputStream(buffer);
            outputStream.writeObject(criteria);
            ByteArrayInputStream input = new ByteArrayInputStream(buffer.toByteArray());
            ObjectInputStream inputStream = new ObjectInputStream(input);
            return (DetachedCriteria)inputStream.readObject();
        } catch (IOException e) {
            logger.warn("复制Criteria对象失败：IO操作失败", e);
        } catch (ClassNotFoundException e) {
            logger.warn("复制Criteria对象失败：未发现匹配的对象类型", e);
        }
        return null;
    }
}
